# "Note that the code we give you is not an example of good design or
# good programming style, whereas the code you hand in should be
# beautiful and excellently modular."
# I'll do this part later

import sys

MEMORY_SIZE = 16

encoding = {
    "HLT": 0b0000, "LDA": 0b0001, "LDI": 0b0010,
    "STA": 0b0011, "STI": 0b0100, "ADD": 0b0101,
    "SUB": 0b0110, "JMP": 0b0111, "JMZ": 0b1000,
}

source = sys.stdin.read()
lines = source.split('\n')

#Strip extra formatting
empty = []
for x in range(len(lines)):
    lines[x] = lines[x].split('#')[0]
    if lines[x].isspace() or not lines[x]:
        empty.append(lines[x])

for x in empty:
    lines.remove(x)

for line in lines:
    print line

#generate a dictionary of labels
#also remove definition labels
labels = {}
for x in range(len(lines)):
    component = lines[x].split(':')
    if len(component) > 2:
        print 'ERROR, more than one colon'
    elif len(component) == 2:
        if component[0] in labels:
            print 'ERROR, multiple definitions for label'
        else:
            labels[component[0]] = x
            lines[x] = component[1].strip()
    else:
        lines[x] = lines[x].strip()
        continue
    
print '\n'
for line in lines:
    print line

#Substitute values for labels
#I'm working under the assumption that you can use a number as a label
#4: DAT 1 for example
#this might be incorrect
for x in range(len(lines)):
    instruction = lines[x].split()
    opcode = instruction[0]
    if len(instruction) > 2:
        print 'ERROR, too many instruction fields'
    elif len(instruction) == 1:
        if opcode == 'HLT':
            continue
        else:
            print 'ERROR, missing address'
    elif len(instruction) == 2:
        address = instruction[1]
        if address in labels:
            instruction[1] = str(labels[address])
        elif address.isdigit():
            break
        else:
            print 'ERROR, undefined label'
    else:
        print 'ERROR, this should be unreachable'
    lines[x] = '\t'.join(instruction)

#Now we should be at loop.z stage
print '\n'
for line in lines:
    print line
        
